using System.Collections;

namespace CSharpPro._1023;

public class Demo
{
    public void TestPrint()
    {
        int[] arr1 = new int[5];
        arr1[1] = 1;
        arr1[2] = 5;
        //Console.WriteLine(arr1[6]);
        for (int i = 0; i < arr1.Length; i++)
        {
            Console.WriteLine(arr1[i] + "\t");
        }

        String[] arr2 = new string[5];
        arr2[0] = "hello";
        arr2[1] = "world";
        for (int i = 0; i < arr2.Length; i++)
        {
            Console.WriteLine(arr2[i] + "\t");
        }

        int[] arr3 = { 1, 2, 3, 4, 5, 6 };
        Console.WriteLine(arr3[4]);

        int[,] arr4 = new int[3, 3];
        arr4[0, 0] = 1;
        arr4[1, 1] = 2;
        arr4[2, 2] = 3;
        int[,] arr5 =
        {
            { 1, 2, 3 },
            { 4, 5, 6 },
            { 6, 7, 8 }
        };
        int[] arr6 = { 55, 88, 66, 77, 44 };
        Array.Sort(arr6);
        foreach (int i in arr6)
        {
            Console.WriteLine(i + "\t");
        }

        Hashtable hashtable = new Hashtable();
        hashtable.Add("name", "张三");
        hashtable.Add("age", "18");
        hashtable.Add("sex", "男");
        foreach (DictionaryEntry entry in hashtable)
        {
            Console.WriteLine(entry.Key + "\t" + entry.Value);
            Console.WriteLine();
        }

        hashtable.Remove(key: "age");
    }

    //作业一
    public static void TestPrint1(int[] array)
    {
        if (array == null || array.Length <= 1)
            return;

        int n = array.Length;
        for (int i = 0; i < n - 1; i++)
        {
            bool swapped = false;
            // 每轮排序后，最大元素会"浮"到末尾，所以j的范围递减
            for (int j = 0; j < n - i - 1; j++)
            {
                if (array[j] > array[j + 1])
                {
                    // 交换元素
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }

            // 如果本轮没有交换，说明数组已有序，提前退出
            if (!swapped)
                break;
        }
    }
}